package TCP.Test;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws Exception {
        Scanner input = new Scanner(System.in);

        while (true) {
            //连接到服务器
            Socket socket = new Socket("127.0.0.1", 8888);
            //发消息
            OutputStream os = socket.getOutputStream();
            System.out.print("请输入密码:");
            String password = input.next();
            os.write(password.getBytes());
            System.out.println("正在登陆中....");
            socket.shutdownOutput();//单向关闭输出流,代表我说完了

            //读取服务器的回复
            InputStream is = socket.getInputStream();//打开输入流
            byte[] b = new byte[1024];
            int len;
            while ((len = is.read(b)) != -1) {
                System.out.println("服务端" + socket.getInetAddress() + "说：");
                String s = new String(b, 0, len);
                System.out.println(s);
                if (s.indexOf("欢迎光临") == -1) {
                    break;
                }else {
                    System.exit(1);
                }
            }
        }
    }
}
